<?php
class Pagination {
	public $total = 0;
  public $version = 0;
	public $page = 1;
	public $limit = 20;
	public $num_links = 10;
	public $url = '';
	public $text = 'Showing {start} to {end} of {total} ({pages} Pages)';
	public $text_first = '|&lt;';
	public $text_last = '&gt;|';
	public $text_next = '&gt;';
	public $text_prev = '&lt;';
	public $style_links = 'links';
	public $style_results = 'results';
  
	public function __construct($version = 0) {
		$this->version = $version;
	} 
  
	public function render() {
		$total = $this->total;
		
		if ($this->page < 1) {
			$page = 1;
		} else {
			$page = $this->page;
		}
		
		if (!(int)$this->limit) {
			$limit = 10;
		} else {
			$limit = $this->limit;
		}
		
		$num_links = $this->num_links;
		$num_pages = ceil($total / $limit);
		
		$output = '';
		$outputprev = '';
		$outputnext = '';
		
		if($this->version){
		  if ($page > 1) {
			$outputprev .= ' <a href="' . str_replace('{page}', $page - 1, $this->url) . '">' . $this->text_prev . '</a> | ';
		  }
		}
		else {
		  if ($page > 1) {
			$output .= ' <a href="' . str_replace('{page}', 1, $this->url) . '">' . $this->text_first . '</a> <a href="' . str_replace('{page}', $page - 1, $this->url) . '">' . $this->text_prev . '</a> ';
		  }
		}

		if ($num_pages > 1 && $this->version == 0) {
			if ($num_pages <= $num_links) {
				$start = 1;
				$end = $num_pages;
			} else {
				$start = $page - floor($num_links / 2);
				$end = $page + floor($num_links / 2);
			
				if ($start < 1) {
					$end += abs($start) + 1;
					$start = 1;
				}
						
				if ($end > $num_pages) {
					$start -= ($end - $num_pages);
					$end = $num_pages;
				}
			}

			if ($start > 1) {
				$output .= ' .... ';
			}

			for ($i = $start; $i <= $end; $i++) {
				if ($page == $i) {
					$output .= ' <b>' . $i . '</b> ';
				} else {
					$output .= ' <a href="' . str_replace('{page}', $i, $this->url) . '">' . $i . '</a> ';
				}	
			}
							
			if ($end < $num_pages) {
				$output .= ' .... ';
			}
		}
		
		if($this->version){
		  if ($page < $num_pages) {
			$outputnext .= ' | <a href="' . str_replace('{page}', $page + 1, $this->url) . '">' . $this->text_next . '</a> ';
		  }
		}
		else {
		  if ($page < $num_pages) {
			$output .= ' <a href="' . str_replace('{page}', $page + 1, $this->url) . '">' . $this->text_next . '</a> <a href="' . str_replace('{page}', $num_pages, $this->url) . '">' . $this->text_last . '</a> ';
		  }
		}
    
		if($this->version){
      $find = array(
        '{current}',
        '{pages}'
      );
    }
    else {
      $find = array(
        '{start}',
        '{end}',
        '{total}',
        '{current}',
        '{pages}'
      );
	}
	
    if($this->version){
	if($page != $num_pages){
		$templink = '<a href="' . str_replace('{page}', $num_pages, $this->url) . '">'.$num_pages.'</a>';
	}
	else {
		$templink = $num_pages;
	}
      $replace = array(
        $page,
        $templink
      );
    }
    else {
      $replace = array(
			($total) ? (($page - 1) * $limit) + 1 : 0,
			((($page - 1) * $limit) > ($total - $limit)) ? $total : ((($page - 1) * $limit) + $limit),
			$total,
			$page,
			$num_pages
		);
    }
    if($this->version){
      $output .= ' .... ';
      return ($output ? '<!div class="' . $this->style_links . '"-->' . $outputprev . str_replace($find, $replace, $this->text) . $outputnext . '<!--/div-->' : '') . ($num_pages != 1 ? ' | <a href="'.str_replace('{page}', 1, $this->url).'&limit=1000">Показать все</a>' : '').'<!--div class="' . $this->style_results . '">' . str_replace($find, $replace, $this->text) . '</div-->';
    }
    else {
      return ($output ? '<div class="' . $this->style_links . '">' . $output . '</div>' : '') . '<div class="' . $this->style_results . '">' . str_replace($find, $replace, $this->text) . '</div>';
    }
	}
}
?>